﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Configuration;
using ServiceStack.Configuration;
using ServiceStack.WebHost.Endpoints;
using ServiceStack.ServiceInterface;
using ServiceStack.ServiceInterface.Cors;
using ServiceStack.ServiceInterface.Auth;
using ServiceStack.ServiceInterface.ServiceModel;
using g42.Services;
using g42.Repos;
using g42.Authentication.NH;

namespace g42.ServiceProvider
{
  /*
  //ASP.NET Hosts
  public class StarterTemplateAppHost
    : AppHostBase
  {
    static readonly ConfigurationResourceManager AppSettings = new ConfigurationResourceManager();

    public StarterTemplateAppHost()
      : base(AppSettings.GetString("ServiceName") ?? "StarterTemplate ASP.NET Host", typeof(HelloService).Assembly) { }

    public override void Configure(Funq.Container container)
    {
      container.Register(new TodoRepository());
    }
  }
  */

  //HttpListener Hosts
  public class StarterTemplateAppListenerHost : AppHostHttpListenerBase
  {
    static readonly ConfigurationResourceManager AppSettings = new ConfigurationResourceManager();

    public StarterTemplateAppListenerHost()
      : base(AppSettings.GetString("ServiceName") ?? "StarterTemplate HttpListener", typeof(SampleService).Assembly) { }

    // Hier werden die Repositories den Services bekanntgemacht (IOC)
    public override void Configure(Funq.Container container)
    {
      // Verhindern von: "XMLHttpRequest cannot load http://ll-faberg:1337/SampleList. Origin http://ll-faberg:9090 is not allowed by Access-Control-Allow-Origin."
      // http://stackoverflow.com/questions/8211930/servicestack-rest-api-and-cors
      // http://joeriks.com/2013/01/12/cors-basicauth-on-servicestack-with-custom-authentication/
      Plugins.Add(new CorsFeature()); //Registers global CORS Headers

      //Enable Authentication
      ConfigureAuth(container);

      //Register all your dependencies
      container.Register(new MandantRepository());
      container.Register(new AdresseRepository());
      container.Register(new StrasseRepository());
      container.Register(new PersonRepository());
      container.Register(new SampleRepository());
    }


    private void ConfigureAuth(Funq.Container container)
    {
      var appSettings = new AppSettings();
      //Default route: /auth/{provider}
      Plugins.Add(new AuthFeature(() => new CustomUserSession(),
        new IAuthProvider[] {
					new AcmeCredentialsAuthProvider(appSettings), 
				})); 
      //Default route: /register
      Plugins.Add(new RegistrationFeature());
      //container.Register<IUserAuthRepository>(c => new NHibernateUserAuthRepository(db.sessionFactory));
      container.Register<IUserAuthRepository>(new NHibernateUserAuthRepository(db.sessionFactory));
      var authRepo = (NHibernateUserAuthRepository)container.Resolve<IUserAuthRepository>();

      // Wenn noch nicht existiert, dann einen default Account anlegen
      string UserID = "";
      if (!authRepo.TryAuthenticate("admin", "g42", out UserID))
      {
        authRepo.CreateUserAuth(new UserAuth { Id = 1, UserName = "admin" }, "g42");
      }

    }
  }
    
}
